package com.leetcode.August;

import javax.swing.tree.TreeNode;

/**
 * @Description: 129. 求根节点到叶节点数字之和
 * @Author: Mr.Fxy
 * @CreateTime: 2024-08-26 10:26
 */
public class LC129求根节点到叶节点数字之和 {

    public int sumNumbers(TreeNode root) {
        if (root == null) return 0;
        return dfs(root, 0);
    }

    public int dfs(TreeNode root, int current) {
        if (root == null) return 0;
        current = current * 10 + root.val;
        if (root.left == null && root.right == null) return current;
        return dfs(root.left, current) + dfs(root.right, current);
    }
}
